package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sggg.entity.SgBlutdruckRecord;
import org.jeecg.modules.sggg.entity.SgDecisionRecord;
import org.jeecg.modules.sggg.service.ISgDecisionRecordService;
import org.jeecg.modules.system.controller.SysRoleController;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 判定记录表
 * @Author: jeecg-boot
 * @Date: 2022-10-26
 * @Version: V1.0
 */
@Api(tags = "判定记录表")
@RestController
@RequestMapping("/sggg/sgDecisionRecord")
@Slf4j
public class SgDecisionRecordController extends JeecgController<SgDecisionRecord, ISgDecisionRecordService> {

    @Autowired
    private ISgDecisionRecordService sgDecisionRecordService;

    @Autowired
    private SysRoleController sysRoleController;

    @Autowired
    private ISysRoleService sysRoleRoleService;

    @Autowired
    private ISysDepartService sysDepartController;

    @Autowired
    private ISysUserService sysUserService;


    @ApiOperation(value = "判定记录表-分页列表查询", notes = "判定记录表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgDecisionRecord>> queryPageList(SgDecisionRecord sgDecisionRecord,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SgDecisionRecord> page = new Page<>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<SysRole> roleList = sysRoleRoleService.getUserRole(sysUser.getId());
        String code = "";
        StringBuilder sb = new StringBuilder();
        for (SysRole role : roleList) {
            sb.append(role.getRoleCode());
            sb.append(",");
        }
        String sbstr = sb.toString();
        if (sbstr.contains("admin") ) {
            code = "admin";
        } else if (sbstr.contains("dept_manage")) {
            //机构管理员
            code = "dept_manage";
        } else {
            code = "other";
        }

        SysDepart sysDepart = sysDepartController.getById(sysUser.getDepartIds());
        SysUser sysUser1 = sysUserService.getById(sysUser.getId());
        if(sysDepart !=null && sysDepart.getDeptAttr()!=null){
            // 1.三高之家
            if(sysDepart.getDeptAttr().equals("1")){
//                queryWrapper.eq("doctor_id", sysUser1.getId().toString());
                sysUser.setId(sysUser1.getId());
            }
            // 2.三高基地
            if(sysDepart.getDeptAttr().equals("2")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                sgDecisionRecord.setResponsibleAgencies(responsibleAgencies);
//                queryWrapper.in("sys_org_code", responsibleAgencies);
            }
            // 3.三高中心
            if(sysDepart.getDeptAttr().equals("3")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                sgDecisionRecord.setResponsibleAgencies(responsibleAgencies);
            }
        }else{
            sysUser.setId(sysUser1.getId());
        }

        IPage<SgDecisionRecord> pageList =   sgDecisionRecordService.pageList(page,
                sgDecisionRecord, sysUser.getId(), code,
                sysUser.getOrgCode());

        return Result.OK(pageList);
    }


    @AutoLog(value = "判定记录表-添加")
    @ApiOperation(value = "判定记录表-添加", notes = "判定记录表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgDecisionRecord sgDecisionRecord) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        sgDecisionRecord.setCreateBy(sysUser.getRealname());

        //低危0 中危1 高危2 极高危3
        //高血压1级
        if (sgDecisionRecord.getBpGrade()!=null&&Strings.isNotBlank(String.valueOf(sgDecisionRecord.getBpGrade()))&&sgDecisionRecord.getBpGrade()==0){
            //无其它危险因素
            if (Strings.isBlank(sgDecisionRecord.getBpDanger())){
                sgDecisionRecord.setBpHazardLevel(0);
            }
            //有其它危险因素
            if (Strings.isNotBlank(sgDecisionRecord.getBpDanger())){
                String[] bgDanger = sgDecisionRecord.getBpDanger().split(",");
                //1~2个危险因素
                if (bgDanger.length<=2){
                    sgDecisionRecord.setBpHazardLevel(1);
                }
                //大于等于3个危险因素或糖尿病或靶器官危害
                if (bgDanger.length>=3||Strings.isNotBlank(String.valueOf(sgDecisionRecord.getBgType()))||Strings.isNotBlank(sgDecisionRecord.getOrgan())){
                    sgDecisionRecord.setBpHazardLevel(2);
                }
            }
        }

        //高血压2级
        if (sgDecisionRecord.getBpGrade()!=null&&Strings.isNotBlank(String.valueOf(sgDecisionRecord.getBpGrade()))&&sgDecisionRecord.getBpGrade()==1){
            //无其它危险因素
            if (Strings.isBlank(sgDecisionRecord.getBpDanger())){
                sgDecisionRecord.setBpHazardLevel(1);
            }
            //有其它危险因素
            if (Strings.isNotBlank(sgDecisionRecord.getBpDanger())){
                String[] bgDanger = sgDecisionRecord.getBpDanger().split(",");
                //1~2个危险因素
                if (bgDanger.length<=2){
                    sgDecisionRecord.setBpHazardLevel(1);
                }
                //大于等于3个危险因素或糖尿病或靶器官危害
                if (bgDanger.length>=3||Strings.isNotBlank(String.valueOf(sgDecisionRecord.getBgType()))||Strings.isNotBlank(sgDecisionRecord.getOrgan())){
                    sgDecisionRecord.setBpHazardLevel(2);
                }
            }
        }

        //高血压3级
        if (sgDecisionRecord.getBpGrade()!=null&&Strings.isNotBlank(String.valueOf(sgDecisionRecord.getBpGrade()))&&sgDecisionRecord.getBpGrade()==2){
            //无其它危险因素
            if (Strings.isBlank(sgDecisionRecord.getBpDanger())){
                sgDecisionRecord.setBpHazardLevel(2);
            }
            //有其它危险因素
            if (Strings.isNotBlank(sgDecisionRecord.getBpDanger())){
                String[] bgDanger = sgDecisionRecord.getBpDanger().split(",");
                //1~2个危险因素
                if (bgDanger.length<=2){
                    sgDecisionRecord.setBpHazardLevel(3);
                }
                //大于等于3个危险因素或糖尿病或靶器官危害
                if (bgDanger.length>=3||Strings.isNotBlank(String.valueOf(sgDecisionRecord.getBgType()))||Strings.isNotBlank(sgDecisionRecord.getOrgan())){
                    sgDecisionRecord.setBpHazardLevel(3);
                }
            }
        }

        sgDecisionRecordService.save(sgDecisionRecord);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "判定记录表-编辑")
    @ApiOperation(value = "判定记录表-编辑", notes = "判定记录表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgDecisionRecord sgDecisionRecord) {
        sgDecisionRecordService.updateById(sgDecisionRecord);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "判定记录表-通过id删除")
    @ApiOperation(value = "判定记录表-通过id删除", notes = "判定记录表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgDecisionRecordService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "判定记录表-批量删除")
    @ApiOperation(value = "判定记录表-批量删除", notes = "判定记录表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgDecisionRecordService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "判定记录表-通过id查询", notes = "判定记录表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgDecisionRecord> queryById(@RequestParam(name = "id") String id) {
        SgDecisionRecord sgDecisionRecord = sgDecisionRecordService.getById(id);
        if (sgDecisionRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgDecisionRecord);
    }

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgDecisionRecord sgDecisionRecord) {
        return super.exportXls(request, sgDecisionRecord, SgDecisionRecord.class, "判定记录表");
    }

    @ApiOperation(value = "查询居民血压测量记录列表", notes = "查询居民血压测量记录列表")
    @GetMapping(value = "/queryByMemberIdList")
    public Result<List<SgDecisionRecord>> queryByMemberIdList(
            @RequestParam(name = "member_id") String member_id) {

        QueryWrapper<SgDecisionRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", member_id);
        queryWrapper.orderByDesc("create_time");
//        IPage<SgBlutdruckRecord> pageList = sgBlutdruckRecordService.page(page, queryWrapper);
        List<SgDecisionRecord> pageList = sgDecisionRecordService.list(queryWrapper);
        if (pageList == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(pageList);
    }

}
